using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;
using CMS.Data;
using CMS.Presenters;
using AjaxPro;

namespace CMS.Web.CompDenu
{
    public partial class SelectDiaBan : System.Web.UI.Page, ISelectDiaBanView
    {
        SelectDiaBanPresenter presenter;
        IList<DiaBan> dbList;

        public String ObjId = "";
        public String HidId = "";
        public int MaDiaBan = 0;
        public Boolean isSelected = false;

        public SelectDiaBan()
        {
            this.presenter = new SelectDiaBanPresenter(this);
            try
            {                
                MaDiaBan = Convert.ToInt32(CauHinhHeThong.MaDiaBan);
            }
            catch (Exception)
            {
                MaDiaBan = 0;
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            trvDiaBan.Attributes.Add("onclick", "return OnTreeClick(event)");
            this.ObjId = (this.Request.QueryString["objId"] == null) ? Convert.ToString(MaDiaBan) : this.Request.QueryString["objId"];
            this.HidId = this.Request.QueryString["hidId"];

            if (!this.IsPostBack)
            {
                BuildTreeRoot(trvDiaBan, this.DiaBanList);            
            }
        }

        #region ISelectDiaBanView Members

        public IList<DiaBan> DiaBanList
        {
            set { this.dbList = value; }
            get { return this.dbList; }
        }

        #endregion

        
        public int getTreeValue()
        {
            int id = 0;
            foreach (TreeNode node in trvDiaBan.Nodes)
            {
                if (node.Checked)
                {
                    id = Convert.ToInt32(node.Value);
                    break;
                }
            }
            return id;
        }

        private void BuildTreeRoot(TreeView trv, IList<DiaBan> dbList)
        {
            trv.Nodes.Clear();
            if (dbList.Count == 0)
                return;
            foreach (DiaBan db in dbList)
            {
                if (db.MaDiaBanTrucThuoc == MaDiaBan)
                {
                    TreeNode nodetmp = new TreeNode(db.TenDiaBan, db.ID.ToString());
                    //nodetmp.NavigateUrl = "#";
                    trv.Nodes.Add(nodetmp);
                    nodetmp.SelectAction = TreeNodeSelectAction.Select;
                    BuildTree(nodetmp, dbList);                    
                }
            }
        }

        private void BuildTree(TreeNode node, IList<DiaBan> dbList)
        {
            if (dbList.Count == 0)
                return;
            foreach (DiaBan db in dbList)
            {
                if (db.MaDiaBanTrucThuoc.ToString() == node.Value)
                {
                    TreeNode nodetmp = new TreeNode(db.TenDiaBan, db.ID.ToString());
                    //nodetmp.NavigateUrl = "#";
                    nodetmp.SelectAction = TreeNodeSelectAction.Select;
                    node.ChildNodes.Add(nodetmp);
                    BuildTree(nodetmp, dbList);                    
                }
            }
        }

        private void CheckExistRoots(TreeView trv, string id)
        {
            foreach (TreeNode node in trv.Nodes)
            {
                if (node.Value == id)
                {
                    node.Checked = true;
                    node.SelectAction = TreeNodeSelectAction.None;
                    //CheckExistNodes(node, dbList);
                }
            }
        }

        private void CheckExistRoot(TreeView trv, IList<DiaBan> dbList)
        {
            foreach (TreeNode node in trv.Nodes)
            {
                foreach (DiaBan db in dbList)
                {
                    if (node.Value == db.ID.ToString())
                    {
                        node.Checked = true;
                        node.SelectAction = TreeNodeSelectAction.None;
                        CheckExistNodes(node, dbList);
                    }
                }
            }
        }

        private void CheckExistNodes(TreeNode parent, IList<DiaBan> dbList)
        {
            foreach (TreeNode node in parent.ChildNodes)
            {
                foreach (DiaBan db in dbList)
                {
                    if (node.Value == db.ID.ToString())
                    {
                        node.Checked = true;
                        CheckExistNodes(node, dbList);
                    }
                }

            }
        }
                      
    }
}
